<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\room;

use app\common\model\BaseModel;
use app\common\model\bill\BillBasicModel;
use app\common\model\contract\ContractBillModel;
use app\common\model\contract\ContractBasicModel;
use app\common\model\property\basic\PropertyUnitModel;
use app\common\model\property\basic\PropertyBasicModel;

class RoomBasicModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'room_basic';
    }

    public function contractInfo()
    {
        return $this->hasOne(ContractBasicModel::class, 'id', 'contract_id');
    }

    public function getLaveDaysAttr()
    {
        return intval((strtotime($this->contractInfo()->value('end_date', date('ymd'))) - strtotime(date('ymd'))) / 86400);
    }

    public function getVacancyDayAttr()
    {
        return $this->status == 1 ? intval((time() - max(strtotime($this->add_time), strtotime($this->vacancy_time))) / 86400) : 0;
    }

    public function getContractStatusAttr()
    {
        return $this->contractInfo()->value('status');
    }

    public function getContractEndDateAttr()
    {
        return $this->contractInfo()->value('end_date');
    }

    public function propertyInfo()
    {
        return $this->hasOne(PropertyBasicModel::class, 'id', 'property_id');
    }

    public function pictureInfo()
    {
        return $this->hasMany(RoomPictureModel::class, 'room_id', 'id')->where('is_show', 1);
    }

    public function getContractRentAttr()
    {
        return $this->hasOne(ContractBillModel::class, 'contract_id', 'contract_id')->where('if_bill', 2)->value('amount');
    }

    public function getContractDepositAttr()
    {
        return $this->hasOne(ContractBillModel::class, 'contract_id', 'contract_id')->where('if_bill', 1)->value('amount');
    }


    public function getPictureAttr()
    {
        return get_arr_column($this->pictureInfo()->field('image_url')->select()->toArray(), 'image_url');
    }

    public function getLabelIdsAttr()
    {
        return RoomBasicLabelModel::where('room_id', $this->id)->column('label_id');
    }

    public function getLabelListAttr()
    {
        if ($this->LabelIds) {
            return RoomLabelModel::whereIn('id', $this->LabelIds)->field('id,label_name')->select()->toArray();
        }
        return [];
    }


    /**
     * 楼栋单元名称
     *
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUnitNameAttr()
    {
        $roomUnitInfo = PropertyUnitModel::getDB()->where('id', $this->unit_id)->find();
        if ($roomUnitInfo) {
            $pids = array_filter(explode('/', $roomUnitInfo['path']));
            $pids[] = $roomUnitInfo['id'];
            $units = PropertyUnitModel::getDB()->where('id', 'in', $pids)->column('unit_name');
            return implode(' - ', $units);
        }
        return '';
    }

    /**
     * 获取完整房间名
     * @return string
     *
     * @author Amos
     * 2024-06-12 23:37
     */
    public function getFullRoomNameAttr()
    {
        $result = ($this->propertyInfo['name'] ?? '') . '';
        if ($this->unit_name) {
            $result = $this->unit_name ? (' - ' . $this->unit_name) : '';
        }
        if ($this->floor_name > 0) {
            $result .= is_numeric($this->floor_name) ? (' - ' . $this->floor_name . '层') : $this->floor_name;
        }
        $result .= ' - ' . $this->room_name . ($this->room_unit ?? '');
        return $result;
    }

    /**
     * 根据楼栋单元  获取完整房间名
     * @return string
     *
     * @author Amos
     * 2024-06-12 23:37
     */
    public function getUnitRoomNameAttr()
    {
        $result = '';
        if ($this->unit_name) {
            $result = $this->unit_name ? ($this->unit_name . ' - ') : '';
        }
        if ($this->floor_name > 0) {
            $result .= is_numeric($this->floor_name) ? ($this->floor_name . '层' . ' - ') : $this->floor_name;
        }
        $result .= $this->room_name . ($this->room_unit ?? '');
        return $result;
    }

    /**
     * 根据房间户型
     * @return string
     *
     * @author Amos
     * 2024-06-12 23:37
     */
    public function getHouseTypeAttr()
    {
        $result = '';
        if ($this->room_num > 0) {
            $result .= $this->room_num ? ($this->room_num . ' 房') : '';
        }
        if ($this->hall_num > 0 && $this->room_num > 0) {
            $result .= $this->hall_num ? ($this->hall_num . ' 厅') : '';
        }
        if ($this->who_num > 0 && $this->room_num > 0) {
            $result .= $this->who_num ? ($this->who_num . ' 卫') : '';
        }
        return $result;
    }

    /**
     * 获取交租状态
     * @return int
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-05-12 22:24
     */
    public function getPayRentStatusAttr()
    {
        $status = 1;
        $billNum = BillBasicModel::where('room_id', $this->id)->where('status', 'in', [2, 5])->count();
        if ($billNum > 0) {
            $status = 2;
        }
        return $status;
    }

}
